class Formatter(object):
    def auth(self, headers, post_body):
        return True

class GoogleCodeFormatter(Formatter):
    #Jinja for templating?
    def __init__(self, format_template="$json", auth_key=None):
        self.format_template = format_template
        self.auth_key = auth_key

    def format(self, post_body):
        import simplejson
        from string import Template
        import time
        print post_body
        json_body = simplejson.loads(post_body)
        print json_body
        messages = []
        for revision in json_body['revisions']:
            messages.append(Template(self.format_template).substitute(
                added=', '.join(revision['added']),
                modified=', '.join(revision['modified']),
                removed=', '.join(revision['removed']),
                author=revision['author'],
                author_no_email=' '.join(revision['author'].split(' ')[:-1]),
                url=revision['url'],
                timestamp=str(revision['timestamp']),
                ago="%ss" % int(time.time() - revision['timestamp']),
                parents=', '.join(revision['parents']),
                path_count=str(revision['path_count']),
                message=revision['message'],
                revision=revision['revision'],
                repository_path=json_body['repository_path'],
                project_name=json_body['project_name'],
                revision_count=json_body['revision_count'],
                json=json_body,
            ))
        return messages

    def auth(self, headers, post_body):
        if self.auth_key:
            import hmac
            m = hmac.new(self.auth_key)
            m.update(post_body)
            digest = m.hexdigest()
            googles_digest = headers.getRawHeaders("Google-Code-Project-Hosting-Hook-Hmac")
            if len(googles_digest) > 0 and digest == googles_digest[0]:
                return True
            else:
                return False
        else:
            return True
